import java.util.Stack;

public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            Character a = s.charAt(i);
            //遇到右括号出栈
            if(a == ')' || a == '}' || a == ']') {
                if(stack.isEmpty()) {
                    return false;
                }
                Character c = stack.peek();
                if(c =='(' && a == ')' || c == '[' && a == ']' || c == '{' && a == '}') {
                    //出栈
                    stack.pop();
                    continue;
                }else {
                    return false;
                }
            }
            stack.push(a);
        }
        //遍历完成,队列有剩余
        if(!stack.isEmpty()) {
            return false;
        }
        return true;
    }
}
